package cn.alpha.intell.rbtlib.wifi.dlalog;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.alpha.intell.rbtlib.R;
import cn.alpha.intell.rbtlib.rbt.utils.ButtonSound;

/**
 * wifi 密码输入 dialog
 */
public class InputWiFiPwDialog extends Dialog {
    public InputWiFiPwDialog(Context context) {
        super(context);
    }

    public InputWiFiPwDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void applyCompat() {
        if (Build.VERSION.SDK_INT < 19) {
            return;
        }
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        applyCompat();
        super.onCreate(savedInstanceState);
    }

    public static class Builder {
        private Context context;
        private String cancelText;
        private String connectText;
        private ImageView cancelTv;
        private ImageButton connectTv;

        private OnInputWiFiPwListener cancelListener;
        private OnInputWiFiPwListener connectListener;
        private EditText inputPw;
        private IHaveFocus haveFocus;
        private View.OnFocusChangeListener onFocusChangeListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setOnFocusListener(View.OnFocusChangeListener onFocusListener) {
            this.onFocusChangeListener = onFocusListener;
            return this;
        }

        /**
         * Set the negative button String and it's listener
         *
         * @param cancelText
         * @param cancelListener
         * @return
         */
        public Builder setCancelText(String cancelText, OnInputWiFiPwListener cancelListener) {
            this.cancelText = cancelText;
            this.cancelListener = cancelListener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @param cancelText
         * @param cancelListener
         * @return
         */
        public Builder setCancelText(int cancelText, OnInputWiFiPwListener cancelListener) {
            this.cancelText = (String) context.getText(cancelText);
            this.cancelListener = cancelListener;
            return this;
        }

        public Builder setHaveFocusListener(IHaveFocus haveFocusListener) {
            this.haveFocus = haveFocusListener;
            return this;
        }

        /**
         * Set the positive button String and it's listener
         *
         * @param connectText
         * @param connectListener
         * @return
         */
        public Builder setConnectText(String connectText, OnInputWiFiPwListener connectListener) {
            this.connectText = connectText;
            this.connectListener = connectListener;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param connectText
         * @param connectListener
         * @return
         */
        public Builder setConnectText(int connectText, OnInputWiFiPwListener connectListener) {
            this.connectText = (String) context.getText(connectText);
            this.connectListener = connectListener;
            return this;
        }

        public InputWiFiPwDialog create() {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final InputWiFiPwDialog dialog = new InputWiFiPwDialog(context, R.style.Dialog);
            View view = inflater.inflate(R.layout.dialog_wifi_input_pw, null);

            dialog.setContentView(view);
            WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
            p.gravity = Gravity.CENTER;
            p.width = 758;
            p.height =  400;
            dialog.getWindow().setAttributes(p);


            cancelTv = (ImageView) view.findViewById(R.id.dialog_input_pw_cancel);

            cancelTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cancelListener != null) {
                        cancelListener.inputWiFiPw("");
                    }
                    dialog.dismiss();
                }
            });

            connectTv = (ImageButton) view.findViewById(R.id.dialog_input_pw_connect);
            connectTv.setEnabled(false);
            connectTv.setBackgroundResource(R.mipmap.but_connect_disable);
            connectTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ButtonSound.playOgg(context);
                    if (connectListener != null) {
                        connectListener.inputWiFiPw(inputPw.getText().toString());
                    }
                    dialog.dismiss();

                }
            });

            inputPw = (EditText) view.findViewById(R.id.dialog_input_pw_edt);
            inputPw.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.toString().trim().length() > 7) {
                        connectTv.setEnabled(true);
                        connectTv.setBackgroundResource(R.mipmap.but_connect_normal);

                    } else {
                        connectTv.setEnabled(false);
                        connectTv.setBackgroundResource(R.mipmap.but_connect_disable);

                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            dialog.setCanceledOnTouchOutside(true);
            return dialog;
        }

        public interface OnInputWiFiPwListener {
            void inputWiFiPw(String password);
        }

        public interface IHaveFocus {
            void HaveFocus(boolean haveFocus, View v);
        }
    }
}
